{% extends "layout.html" %}
{% block title %}仓位检查{% endblock %}
{% block head %}
  {{ super() }}

{% endblock %}
{% block content %}
    <form class="form-inline" role="form" id="PositionSelectFrom">

                 <div class="form-group"><!-- 用户选择-->
                        账户
                     <select name="userlogic" id="logicid" class="form-control">
                                                <option value ="包括" selected = "selected">包括</option>
                                                <option value ="排除" >排除</option>
                        </select>

                        <select name="users" id="select_users" class="form-control">

                        </select>
                 </div>

                <div class="form-group"> <!--商品选择-->
                    商品
                    <div class="form-group">
                        <select name="instrumentlogic" id="logicid" class="form-control">
                                                <option value ="包括" selected = "selected">包括</option>
                                                <option value ="排除" >排除</option>
                        </select>

                        <select name="select_instrument" id="select_instrument" class="form-control"></select>
                    </div>
                </div>


                <button type="button" class="btn btn-default" onclick="selectposition()">查询</button>
                <button type="button" class="btn btn-default" onclick="synchronous()">同步</button>
    </form>


    <div>
            <table class="table table-bordered table-striped table-condensed table-hover" id="position">
		        <caption></caption>
		           <thead></thead>
		           <tbody></tbody>
	        </table>
    </div>

    <script>
     var table_cols = {};
     var table_tr_title= {};
    //table_cols['position'] = new Array("策略", "商品", "映射商品", "周期", "参数", "交易", "交易状态", "当前理论仓位");
    table_cols['position'] = new Array("AccountId", "Instrument", "AccountPosition","StrategyPosition","disparity","AccountBuy","StrategyBuy",'AccountShort','StrategyShort');
    table_tr_title['position'] = new Array( "账户","合约", "实盘净","策略净","仓差","实盘多","策略多",'实盘空','策略空');
    //页面加载启动
    $(document).ready(function () {

        var allInstrument={{allInstrument|tojson}}//从数据库得到所有合约以及最小跳
        var userslist={{userlist|tojson}}//从数据库得到所有合约以及最小跳
        //init_table('strateg');
        init_table('position');   //列名进行填充
        //初始化下拉选框
        selesuser = document.getElementById('select_users');  //加载合约
        selesuser.options.add(new Option("全部","全部")); //策略标识作为返回的值
        for(var i=0; i<userslist.length; i++){
            selesuser.options.add(new Option($.trim(userslist[i])), $.trim(userslist[i])); //策略标识作为返回的值
        }

        seles = document.getElementById('select_instrument');  //加载合约
        seles.options.add(new Option("全部","全部")); //策略标识作为返回的值
        for(var i=0; i<allInstrument.length; i++){
            seles.options.add(new Option($.trim(allInstrument[i]['InstrumentID'])), $.trim(allInstrument[i]['InstrumentID'])); //策略标识作为返回的值
        }
		});

  //对列名进行填充
    function init_table(table_name) {
        var cols = table_tr_title[table_name];

        //用html()或$("#xx")[0].innerHTML
        $("#"+table_name + " thead").html('<tr></tr>');
        var title = $("#"+table_name + " thead tr")

        for(var v in cols){
            title.append('<th>'+cols[v]+'</th>');
        }
    }

    //查询仓位事件
    function selectposition(){
        var data={}
        data= $('#PositionSelectFrom').serialize();
        $.post('selectPosition',data,function(result){
           if(result.code!='success'){
               alert("failed to create a new api");
           } else{
                $("#position tbody").empty();    //初始化删除所有的元素
               for (i in result.obj){
                   for(key in result.obj[i]){
                       table_show('position',result.obj[i][key]['AccountId']+key, result.obj[i][key]);
                   }
               }
           }
        });
    }
    //对仓位进行同步
     function synchronous(){
        var data={}
        data= $('#PositionSelectFrom').serialize();
        $.post('synchronous',data,function(result){
           if(result.code!='success'){
               alert('同步出现错误');
           }else{
               alert('同步完成');
           }
        });
     }
    //填充数据
    function table_show(table_name,key,data){
        var cols=table_cols[table_name];
        //var key= null;
        switch(table_name){   //根据不同的table 查找相应的key
            case "position":
                key=key
        }

        var tr = $("#"+table_name + ' tbody').children("#"+key);

        if(tr.length==0){
            $("#"+table_name + ' tbody')[0].innerHTML += '<tr id="' + key + '"></tr>';
            tr = $("#"+table_name + " tbody #"+key);
            for(var v in cols){
                var val = data[cols[v]];   //  遍历单行 每列的数据
                if(typeof(val) == "number")// !isNaN(val)) //数字
						val = val.toFixed(2);
					tr.append('<td id="'+cols[v]+'">' + val + '</td>');
					//对于普通数据的css进行更改
					if(!isNaN(data[cols[v]])) //数字:右对齐
						tr.children("#"+cols[v]).css('text-align', 'right');
            }
        }else{
            tr=$("#"+ table_name + "tbody #"+ key);
            for(var v in cols){
                var val= data[cols[v]];
                if(typeof(val)== "number" ){
                    val=val.toFixed(2);
                }
                tr.children("#"+cols[v]).first().text(val);

            }
        }
    }
    </script>
{% endblock %}